package fr.istic.linkedcv.client.presentation.views.impl;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.services.PersonneService;
import fr.istic.linkedcv.client.services.PersonneServiceAsync;

public class MessageForm extends View{
	private Label titreLabel = new Label("Titre : ");
	private TextBox titre = new TextBox();
	private Label messageLabel = new Label("Message");
	private TextArea message = new TextArea();
	private Button envoyerBtn = new Button("Envoyer");
	private VerticalPanel vPanel;
	
	private final PersonneServiceAsync personneServiceAsync = GWT.create(PersonneService.class);
	public MessageForm(Ui ui){
		super(ui);
	}

	@Override
	public void addProperties(Map<String, String> properties) {
		// TODO Auto-generated method stub
		super.addProperties(properties);
	}

	@Override
	public void addProperty(String key, String value) {
		// TODO Auto-generated method stub
		super.addProperty(key, value);
	}

	@Override
	public void build() {
		vPanel = new VerticalPanel();
		vPanel.add(titreLabel);
		vPanel.add(titre);
		vPanel.add(messageLabel);
		vPanel.add(message);
		vPanel.add(envoyerBtn);
		vPanel.setWidth("1000px");
		vPanel.addStyleName("message-form");
		
		if(properties.get("parameter1")==null){
			envoyerBtn.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					envoyerMessageToAllFriendsClickHandler();
					
				}
			});
		}else{
			envoyerBtn.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					EnvoyerMessageToFriendClickHandler(properties.get("parameter1"));
					
				}
			});
		}
		
	}

	private void envoyerMessageToAllFriendsClickHandler() {
		Map<String, String> messageMap = createMessageMap();
		
		personneServiceAsync.envoyerMessageToAllFriends(messageMap, new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				ui.addHistoryItem("liste_messages");
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private Map<String, String> createMessageMap() {
		Map<String, String> messageMap = new HashMap<String, String>();
		messageMap.put("titre", titre.getValue());
		messageMap.put("message", message.getValue());
		return messageMap;
	}

	private void EnvoyerMessageToFriendClickHandler(String friendName) {
		Map<String, String> messageMap = createMessageMap();
		personneServiceAsync.envoyerMessageToFriend(messageMap, friendName, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) {
				ui.addHistoryItem("liste_messages");
				
			}
		});
		
	}

	@Override
	public Widget getWidget() {
		// TODO Auto-generated method stub
		return vPanel;
	}

}
